use std::env;

pub struct Env {
    // 代码解析
    pub code_llm_endpoint: String,
    pub code_llm_model: String,
    pub code_llm_api_key: String,
    pub code_llm_thread_number: usize,

    // 对话
    pub llm_endpoint: String,
    pub llm_model: String,
    pub llm_api_key: String,

    // 嵌入式模型
    pub embedding_endpoint: String,
    pub embedding_model: String,
    pub embedding_api_key: String,

    // 重排序模型
    pub rerank_endpoint: String,
    pub rerank_model: String,

    // 代码补全
    pub code_completion_endpoint: String,
    pub code_completion_system: Option<String>,
    pub code_completion_model: Option<String>,
    pub code_completion_port: u32,

    // 搜索结果的数量
    pub search_count: usize,
    pub code_hight_theme: String,

    // 字符串分段长度
    pub part_expect_length: usize,

    // mcp
    pub stdio_mcps: Vec<String>,
}

impl Env {
    pub fn init() -> Env {
        // 代码解析
        let code_llm_endpoint =
            env::var("CODE_LLM_ENDPOINT").unwrap_or("https://api.siliconflow.cn/v1".to_string());
        let code_llm_model =
            env::var("CODE_LLM_MODEL").unwrap_or("Qwen/Qwen2.5-Coder-7B-Instruct".to_string());
        let code_llm_api_key = env::var("CODE_LLM_API_KEY").unwrap_or("".to_string());
        let code_llm_thread_number = env::var("CODE_LLM_THREAD_NUMBER")
            .unwrap_or("20".to_string())
            .parse::<usize>()
            .unwrap_or(20);

        // 对话
        let llm_endpoint =
            env::var("LLM_ENDPOINT").unwrap_or("https://api.siliconflow.cn/v1".to_string());
        let llm_model = env::var("LLM_MODEL").unwrap_or("Qwen/Qwen3-8B".to_string());
        let llm_api_key = env::var("LLM_API_KEY").unwrap_or("".to_string());

        // 嵌入式模型
        let embedding_endpoint =
            env::var("EMBEDDING_ENDPOINT").unwrap_or("http://homed:11435".to_string());
        let embedding_model = env::var("EMBEDDING_MODEL").unwrap_or("BAAI/bge-m3".to_string());
        let embedding_api_key = env::var("EMBEDDING_API_KEY").unwrap_or("".to_string());

        // 重排序模型
        let rerank_endpoint =
            env::var("RERANK_ENDPOINT").unwrap_or("http://homed:11435/rerank".to_string());
        let rerank_model = env::var("RERANK_MODEL").unwrap_or("".to_string());

        // 代码补全
        let code_completion_endpoint =
            env::var("CODE_COMPLETION_ENDPOINT").unwrap_or("http://home:11434".to_string());
        let code_completion_system: Option<String> = env::var("CODE_COMPLETION_SYSTEM").ok();
        let code_completion_model: Option<String> = env::var("CODE_COMPLETION_MODEL").ok();
        let code_completion_port = env::var("CODE_COMPLETION_PORT")
            .unwrap_or("11436".to_string())
            .parse::<u32>()
            .unwrap_or(11436);

        // 搜索结果的数量
        let search_count = env::var("SEARCH_COUNT")
            .unwrap_or("20".to_string())
            .parse::<usize>()
            .unwrap_or(20);
        let code_hight_theme =
            env::var("CODE_HIGHT_THEME").unwrap_or("base16-eighties.dark".to_string());

        // 字符串分段长度
        let part_expect_length = env::var("PART_EXPECT_LENGTH")
            .unwrap_or("500".to_string())
            .parse::<usize>()
            .unwrap_or(500);

        // stdio格式的mcp
        let stdio_mcps = env::var("STDIO_MCPS")
            .unwrap_or("".to_string())
            .split(",")
            .into_iter()
            .map(|item| item.to_string())
            .collect();

        Env {
            code_llm_endpoint,
            code_llm_model,
            code_llm_api_key,
            code_llm_thread_number,
            llm_endpoint,
            llm_model,
            llm_api_key,
            embedding_endpoint,
            embedding_model,
            embedding_api_key,
            rerank_endpoint,
            rerank_model,
            code_completion_endpoint,
            code_completion_system,
            code_completion_model,
            code_completion_port,
            search_count,
            code_hight_theme,
            part_expect_length,
            stdio_mcps,
        }
    }
}
